package cn.itcast.videoclient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.itcast.domain.Student;
import cn.itcast.service.VideoService;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
    private ListView listView;
    
    private Button mybutton ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mybutton= (Button)findViewById(R.id.button01);
        listView = (ListView)this.findViewById(R.id.listView);
        listView.setVisibility(View.GONE);   
        mybutton.setOnClickListener(new btn_click());   
    }
    
    class btn_click implements OnClickListener{   
        public void onClick(View arg0) {  
        	listView.setVisibility(View.VISIBLE);   
        	mybutton.setVisibility(View.GONE);   
        	  try {
      			List<Student> students = VideoService.getLastStudent();
      			List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
      			for(Student student : students){
      				HashMap<String, Object> item = new HashMap<String, Object>();
      				item.put("id", student.getId());
      				item.put("name", student.getName());
      				item.put("age", student.getAge());
      				data.add(item);
      			}
      			SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.item,new String[]{"name", "age"}, new int[]{R.id.name, R.id.age});
      			listView.setAdapter(adapter);
      		} catch (Exception e) {
      			e.printStackTrace();
      			Toast.makeText(MainActivity.this, "==========", 1).show();
      			Log.e("MainActivity", e.toString());
      		} 
        }   
    }
}